//
//  gpuImageCameraViewController.m
//  iSeeColor
//
//  Created by narbeh shahnazarian on 12-11-10.
//  Copyright (c) 2012 Wyatt Gosling. All rights reserved.
//

#import "gpuImageCameraViewController.h"
#import "GPUImage.h"
#import "Storage.h"
#import <AssetsLibrary/AssetsLibrary.h>
#import "daltFilter.h"

@interface gpuImageCameraViewController ()
{
    // a camera object
    GPUImageStillCamera *stillCamera;
    
    /// a filter object
    GPUImageFilter *filter;

}

@end

@implementation gpuImageCameraViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    //set the filter to be the daltonization filter
    filter = [[daltFilter alloc] init];

    //filter = [[GPUImageFilter alloc] initWithFragmentShaderFromFile:@"Daltonize"];
    
    
    //prepare the filter for image capturing purposes
    [filter prepareForImageCapture];
    
    //make the view a GPUImage view to present the filtered contents of the camera lense
    GPUImageView *filterView = (GPUImageView *)self.view;
    
    
    [filter forceProcessingAtSize:filterView.sizeInPixels];
    //chain filter to filterView
    [filter addTarget:filterView];
    
    // Create custom GPUImage camera
    stillCamera = [[GPUImageStillCamera alloc] init];
    
    //make orientation of the camera lense refelect current orientation
    stillCamera.outputImageOrientation = UIInterfaceOrientationPortrait;//self.interfaceOrientation;
    
    //chain camera to filter
    [stillCamera addTarget:filter];
    
    // Begin showing video camera stream
    [stillCamera startCameraCapture];
    
    
	// Do any additional setup after loading the view.
}

//Save photo taken
-(IBAction)cameraSnapShot:(id)sender
{    
    //we disable the button so multiple shots do not interrupt image processing/saving
    UIButton *captureButton = (UIButton *)sender;
    captureButton.enabled = NO;
    //CameraSound_iSeeColor_1
    

    
    [stillCamera capturePhotoAsJPEGProcessedUpToFilter:filter withCompletionHandler:^(NSData *processedJPEG, NSError *error){
        UIImage *image = [[UIImage alloc] initWithData:processedJPEG];
        [[Storage getInstance] save:image];
        
        //here we must try and prevent a deadlock from happening if the user rapidly presses the capture photo button.
        runOnMainQueueWithoutDeadlocking(^{
            //                 report_memory(@"Operation completed");
            //we renable the capture button after we have processed/saved the image
            [captureButton setEnabled:YES];
        });

    
    }];
    
    
 
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)viewWillDisappear:(BOOL)animated
{
    // Note: We need to stop camera capture before the view goes off the screen in order to prevent a crash from the camera still sending frames
    [stillCamera stopCameraCapture];
    
    //we remove all targets from the stillCamera and filter
    [stillCamera removeAllTargets];
    [filter removeAllTargets];
   // NSLog(@"i got to this point");
    [super viewWillDisappear:animated];
}



@end
